Programming for Absolute Beginners by 2023
Author:2023
Language: eng
Format: epub
Chapter 11 GroupinG Values toGether with objeCts and arrays
If you need to access the last element of the my_array array, you can do it with
my_array[my_array.length - 1]. The first element is always my_array[0]. Also, if you want to get rid of elements at the end of the array, you can also set the length property.
To get rid of all elements of the array my_array, just do my_array.length = 0;.
PRACTICE QUESTIONS
1. type out the entire largest_age function. test it by sending it different arrays and making sure it always returns the largest age.
2. instead of creating the array values yourself, have the user type the values.
For the first time around, have the user type in exactly three values. Make sure
these values get converted to numbers before storing them in the array!
3. extend your program so that the user can type in as many values as they want.
remember to include a way that the user can indicate that they are finished
either by asking them if they are done after each one or asking the user to type
a special sentinel value that indicates that they are done.
11.5 Mixing Objects and Arrays
While objects and arrays are pretty powerful in their own right, you can increase the power of both of them by mixing them together. The values held in object properties donât have to be just numbers and stringsâthey can be any JavaScript value including other objects or arrays! Likewise, array values donât have to just be numbers; they can be any value that JavaScript supports, including objects and other arrays. Mixing and matching objects and arrays gives you the flexibility to represent just about any real-world data set.
For instance, letâs say that I wanted to put more information about my children than just their age. I also wanted to include their name and favorite color. In addition, letâs say that I really want the list of children to be on a larger record about me, not just sitting by itself in a variable. How would I write that? Figure 11-4 shows how you would write it the long way.
154
Chapter 11 GroupinG Values toGether with objeCts and arrays
Figure 11-4. Building a Complex Object
As you can see, we start out with a blank object in the variable my_record. We then add a name to the object. Next, we add an empty array to the object, and we name the property children. Now, to access this array, we have to use my_record.children.
Since my_record.children is an array, we can use indexes on it. Therefore, my_record.
children[0] refers to the first value in the my_record.children array. Since that index doesnât exist yet, it gets created. But what is stored there? Itâs a new, blank object!
Therefore, my_record.children[0] now refers to an empty object. What can we do with objects? We can add properties. We can create a name property on this new, empty object by doing my_record.children[0].name = "Jim";. We then set the age in the same way.
At the end of this process, we have an object that has an array of objects.
Download
This site does not store any files on its server. We only index and link to content provided by other sites. Please contact the content providers to delete copyright contents if any and email us, we'll remove relevant links or contents immediately.
C | C++ |
Tutorials | Visual C++ |
Hello! Python by Anthony Briggs(9867)
OCA Java SE 8 Programmer I Certification Guide by Mala Gupta(9757)
The Mikado Method by Ola Ellnestam Daniel Brolund(9747)
Algorithms of the Intelligent Web by Haralambos Marmanis;Dmitry Babenko(8258)
Sass and Compass in Action by Wynn Netherland Nathan Weizenbaum Chris Eppstein Brandon Mathis(7745)
Test-Driven iOS Development with Swift 4 by Dominik Hauser(7741)
Grails in Action by Glen Smith Peter Ledbrook(7667)
The Well-Grounded Java Developer by Benjamin J. Evans Martijn Verburg(7517)
Windows APT Warfare by Sheng-Hao Ma(6502)
Secrets of the JavaScript Ninja by John Resig Bear Bibeault(6378)
Layered Design for Ruby on Rails Applications by Vladimir Dementyev(6248)
Blueprints Visual Scripting for Unreal Engine 5 - Third Edition by Marcos Romero & Brenden Sewell(6117)
Kotlin in Action by Dmitry Jemerov(5019)
Hands-On Full-Stack Web Development with GraphQL and React by Sebastian Grebe(4297)
Functional Programming in JavaScript by Mantyla Dan(4018)
Solidity Programming Essentials by Ritesh Modi(3839)
WordPress Plugin Development Cookbook by Yannick Lefebvre(3614)
Unity 3D Game Development by Anthony Davis & Travis Baptiste & Russell Craig & Ryan Stunkel(3565)
The Ultimate iOS Interview Playbook by Avi Tsadok(3532)
